package com.crealth;


import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.crealth.thread.ImportClienteleDetailThread;


public class textMain {
	private static Logger logger = Logger.getLogger("running_log");
	private static Logger errorLog = Logger.getLogger("error_log");
	
	
	public static void main(String[] args) {
		
		Sheet sheet;
        Workbook book;
        Cell curCell;
        try {
        	String strFileName = "./phone_fee.xlsx";
        	InputStream is = new FileInputStream(strFileName);      	
            book = WorkbookFactory.create(is);
            if (book == null) {
            	errorLog.error(strFileName + "book is null");
            	return;
            }            
            sheet = book.getSheetAt(0);    
            int nRow = 0;
            if (sheet == null || sheet.getLastRowNum() < 3) {
            	errorLog.error(strFileName + "sheet is null");
            	return;
            }            
            
            String strValue = "";
            int rowSum = sheet.getLastRowNum();
            Map<String, Double> phoneFeeMap = new HashMap<String, Double>();            
            List<String> selfPhonels = new ArrayList<String>();
            for (nRow = 0; nRow <= rowSum; ++nRow) {
            	Row rowObj = sheet.getRow(nRow);
            	            	
                curCell = rowObj.getCell(0);                	
                strValue = curCell.toString().trim();                
                Cell cell2 = rowObj.getCell(1);
                double dValue = Double.valueOf(cell2.toString().trim());
                if (phoneFeeMap.containsKey(strValue)) {
                	logger.error("exist " + strValue);
                } else {
                	phoneFeeMap.put(strValue, dValue);
                }
                curCell = rowObj.getCell(2);
                if (curCell != null) {
	                strValue = curCell.toString().trim();
	                if (strValue.startsWith("1.") && strValue.indexOf("E") > 0) {
	                	strValue = ImportClienteleDetailThread.doublePhoneNumToString(strValue);
	                }
	                if (strValue.length() > 0) {
	                	selfPhonels.add(strValue);
	                }
                }
            }
            
            double sumFee = 0.0f;
            for (String phoneNum : selfPhonels) {
            	double curFee = 0.0f;            	
        		String strnum2 = phoneNum.substring(0, 3) + "****" + phoneNum.substring(7);
        		if ( phoneFeeMap.containsKey(strnum2) ) {
            		curFee = phoneFeeMap.get(strnum2);
            		logger.info(phoneNum + "   " + curFee);
            		phoneFeeMap.remove(strnum2);
            	} else {
            		logger.info(phoneNum + "  not found fee ");
            	}
            	sumFee += curFee;
            }
            logger.info("sumFee: " + sumFee);
            
            sumFee = 0.0f;            
            for (Map.Entry<String, Double> entry : phoneFeeMap.entrySet()) {
            	double curFee = entry.getValue();
            	errorLog.info(entry.getKey() + "    " + curFee);
            	sumFee += curFee;           	
            }
            errorLog.info("sumFee: " + sumFee);
        } catch(Exception e) {
        	errorLog.error("", e);
        }
		
	}	
	
	
}
